Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Support Generated Column rewrite in complex Query #50398

Merged
merged 1 commit into from
Sep 9, 2024

Conversation

srlch
Copy link
Contributor

@srlch srlch commented Aug 29, 2024

In this pr:

  1. Refactor Generated Column rewrite code:
    a. Collect generated column expr mapping From QueryAnalyzer.
    b. Introduce ReplaceScalarOperatorRule to replace the ScalarOperator match the generated column expression.
  2. Supprot View, JOIN with Subquery and CTE for Generated Column rewriting.
  3. Support simple equivalent expression rewrite for generated column.

Note: All query structure support Generated Column rewriting obey a simple rule: the Generated Column and its ref columns output directly into the outer scope.

Fixes #issue

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.3
    • 3.2
    • 3.1
    • 3.0
    • 2.5

@srlch srlch requested review from a team as code owners August 29, 2024 02:52
@mergify mergify bot assigned srlch Aug 29, 2024

GeneratedColumnVisitor visitor = new GeneratedColumnVisitor();
visitor.process(node, viewScope);

return viewScope;
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can I help you today?

public Map<Expr, SlotRef> getGeneratedExprToColumnRef() {
return generatedExprToColumnRef;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
return visitor.visitRelation(this, context);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most risky bug in this code is:
Potential thread-safety issues with generatedExprToColumnRef.

You can modify the code like this:

import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
//...

private Map<Expr, SlotRef> generatedExprToColumnRef = new ConcurrentHashMap<>();

public void setGeneratedExprToColumnRef(Map<Expr, SlotRef> generatedExprToColumnRef) {
    this.generatedExprToColumnRef = Collections.synchronizedMap(generatedExprToColumnRef);
}

@srlch srlch force-pushed the gc_column_rewrite2 branch 3 times, most recently from 784e377 to 7583580 Compare August 29, 2024 09:05
@srlch srlch changed the title [Enhancement] Support Generated Column rewrite from View and JOIN with Subquery [Enhancement] Support Generated Column rewrite in complex Query Aug 29, 2024
@srlch srlch force-pushed the gc_column_rewrite2 branch 2 times, most recently from f8b5ab8 to 05deed6 Compare September 6, 2024 05:39
@srlch srlch requested a review from a team as a code owner September 6, 2024 05:39
Seaven
Seaven previously approved these changes Sep 6, 2024
In this pr:
1. Refactor Generated Column rewrite code:
   a. Collect generated column expr mapping From QueryAnalyzer.
   b. Introduce ReplaceScalarOperatorRule to replace the ScalarOperator match the
      generated column expression.
2. Supprot View, JOIN with Subquery and CTE for Generated Column rewriting.
3. Support simple equivalent expression rewrite for generated column.

Note: All query structure support Generated Column rewriting obey a simple rule:
the Generated Column and its ref columns output directly into the outer scope.

Signed-off-by: srlch <[email protected]>
Copy link

sonarcloud bot commented Sep 6, 2024

Copy link

github-actions bot commented Sep 6, 2024

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

github-actions bot commented Sep 6, 2024

[FE Incremental Coverage Report]

pass : 127 / 132 (96.21%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/sql/analyzer/QueryAnalyzer.java 98 103 95.15% [173, 204, 206, 258, 270]
🔵 com/starrocks/sql/optimizer/rewrite/scalar/ReplaceScalarOperatorRule.java 8 8 100.00% []
🔵 com/starrocks/sql/ast/Relation.java 4 4 100.00% []
🔵 com/starrocks/sql/optimizer/transformer/QueryTransformer.java 8 8 100.00% []
🔵 com/starrocks/sql/optimizer/transformer/ExpressionMapping.java 4 4 100.00% []
🔵 com/starrocks/sql/optimizer/transformer/OptExprBuilder.java 1 1 100.00% []
🔵 com/starrocks/sql/optimizer/rewrite/ScalarOperatorRewriter.java 2 2 100.00% []
🔵 com/starrocks/sql/optimizer/transformer/SqlToScalarOperatorTranslator.java 2 2 100.00% []

Copy link

github-actions bot commented Sep 6, 2024

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

@murphyatwork murphyatwork merged commit 96b97a7 into StarRocks:main Sep 9, 2024
53 checks passed
Copy link

github-actions bot commented Sep 9, 2024

@Mergifyio backport branch-3.3

@github-actions github-actions bot removed the 3.3 label Sep 9, 2024
Copy link
Contributor

mergify bot commented Sep 9, 2024

backport branch-3.3

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Sep 9, 2024
Signed-off-by: srlch <[email protected]>
(cherry picked from commit 96b97a7)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/sql/analyzer/QueryAnalyzer.java
srlch added a commit to srlch/starrocks that referenced this pull request Sep 9, 2024
srlch added a commit to srlch/starrocks that referenced this pull request Sep 9, 2024
srlch added a commit to srlch/starrocks that referenced this pull request Sep 9, 2024
srlch added a commit to srlch/starrocks that referenced this pull request Sep 9, 2024
murphyatwork pushed a commit that referenced this pull request Sep 9, 2024
murphyatwork pushed a commit that referenced this pull request Sep 9, 2024
HangyuanLiu pushed a commit to HangyuanLiu/starrocks that referenced this pull request Sep 12, 2024
srlch added a commit to srlch/starrocks that referenced this pull request Oct 11, 2024
Why I'm doing:
This problem is introduce by pr #StarRocks#50398. When we re-analyze the generated expression
in current scope for subquery, sometime we may meet an exception. In current implementation,
a map will be clear but the iteration does not break cause ConcurrentModificationException.

What I'm doing:
Remove the expression if re-analyze throw an exception instead of clear all generated column
expression.

Signed-off-by: srlch <[email protected]>
srlch added a commit to srlch/starrocks that referenced this pull request Nov 4, 2024
…relation if left table and right table has the same column name

Why I'm doing:
This problem is introduced by pr StarRocks#50398. In this pr, we introduce some new rule for generated column rewriting.
The basic idea is following:
1. Collect the rewriting relation in every SELECT scope in query. (Expr -> ColumnRef)
2. Translate the expression relation into Operator mapping: ScalarOperator -> ColumnRefOperator
3. Introduce new rule says ReplaceScalarOperatorRule, use this new rule to replace the ScalarOperator
   into ColumnRefOperator when generating the logical plan in optimizer.

This problem is that, ReplaceScalarOperatorRule use ScalarOperator.isEquivalent to check if a ScalarOperator
is hit the rule instead of using ScalarOperator.equals. ScalarOperator.isEquivalent does not check the operator
id but this id will be used to indentify the column with the same column name but come from different table in JOIN
relation. (e.g column xx in TABLE A and column xx in TABLE B has same name but different id, in this case, ScalarOperator.isEquivalent
return true but ScalarOperator.equals return false). So in this case, we will get the wrong mapping and generated a
incorrent plan for generated column rewrite.

What I'm doing:
1. Using ScalarOperator.equals instead
2. Introduce session variables disable_generated_column_rewrite for disable the generated column rewrite if we want.

Signed-off-by: srlch <[email protected]>
srlch added a commit to srlch/starrocks that referenced this pull request Nov 4, 2024
…relation if left table and right table has the same column name

Why I'm doing:
This problem is introduced by pr StarRocks#50398. In this pr, we introduce some new rule for generated column rewriting.
The basic idea is following:
1. Collect the rewriting relation in every SELECT scope in query. (Expr -> ColumnRef)
2. Translate the expression relation into Operator mapping: ScalarOperator -> ColumnRefOperator
3. Introduce new rule says ReplaceScalarOperatorRule, use this new rule to replace the ScalarOperator
   into ColumnRefOperator when generating the logical plan in optimizer.

This problem is that, ReplaceScalarOperatorRule use ScalarOperator.isEquivalent to check if a ScalarOperator
is hit the rule instead of using ScalarOperator.equals. ScalarOperator.isEquivalent does not check the operator
id but this id will be used to indentify the column with the same column name but come from different table in JOIN
relation. (e.g column xx in TABLE A and column xx in TABLE B has same name but different id, in this case, ScalarOperator.isEquivalent
return true but ScalarOperator.equals return false). So in this case, we will get the wrong mapping and generated a
incorrent plan for generated column rewrite.

What I'm doing:
1. Using ScalarOperator.equals instead
2. Introduce session variables disable_generated_column_rewrite for disable the generated column rewrite if we want.

Signed-off-by: srlch <[email protected]>
renzhimin7 pushed a commit to renzhimin7/starrocks that referenced this pull request Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants